﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Model;
using V5_EShop._Class.BLL.GZ;

namespace V5_EShop
{
    public partial class SubmitBuyerList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var ItemList = (List<BuyerItem>)Session["BuyerItemList"];
            var user = (V5_Model.ModelGZUser)Session["isLogin"];
            var remark = Request["c"] + string.Empty;
            var u = Request["u"] + string.Empty;
            int state = 0;
            decimal totalScore = 0;

            if (user == null)
            {
                WriteMsg("alert('请先登录！');window.location.href='/index.aspx?t=login'");
            }

            if (ItemList == null || ItemList.Count <= 0)
            {
                WriteMsg("alert('请先购物！');window.location.href='/index.aspx'");
            }

            Guid ID = System.Guid.NewGuid();
            var ProdModel = new _Class.Model.ModelGZProduct();

            new BLLGZProductOrder().Add(new ModelGZProductOrder()
            {
                ID = ID,
                BillNo = GetBillNo(),
                CustomID = user.ID,
                CreateDatetime = DateTime.Now,
                Remark = remark,
                PayStatus = state
            });


            foreach (var item in ItemList)
            {
                ProdModel = new V5_EShop._Class.BLL.BLLGZProduct().GetModel(System.Guid.Parse(item.ID.ToUpper()));
                if (ProdModel != null)
                {
                   // ProdModel.IntegralSign = ProdModel.IntegralSign ?? false;

                    decimal prices =  new V5_EShop._Class.BLL.BLLGZProduct().GetPrice(user,ProdModel);

                    new BLLGZProductOrderItem().Add(new ModelGZProductOrderItem()
                    {
                        ParentID = ID,
                        ProductID = System.Guid.Parse(item.ID),
                        OrderPrice = prices,
                        OrderNumber = item.Amount,
                        OrderMoney = prices * item.Amount,
                        //Remark = ProdModel.IntegralSign.Value ? "此商品为活动中心产品" : ""

                    });
                    totalScore += ProdModel.GiftsIntegral.Value;
                }
            }

            if (u == "1" && user.ExistingIntegral > 0 && totalScore > 0 && user.ExistingIntegral >= totalScore)
            {
                state = 1;
                var t = new BLLGZProductOrder().GetModel(ID);
                if (t != null)
                {
                    t.PayStatus = state;
                    new BLLGZProductOrder().Update(t);
                }
                user.ExistingIntegral = user.ExistingIntegral - totalScore;
                new BLLGZUser().Update(user);

            }


            Session["BuyerItemList"] = null;

            WriteMsg("alert('订单提交成功！');window.location.href='/index.aspx?t=user/MyBill'");



        }

        private void WriteMsg(string info)
        {
            Response.Write("<script>" + info + "</script>");
            Response.End();
        }

        /// <summary>
        /// 获取单号
        /// </summary>
        /// <param name="billTableName"></param>
        /// <returns></returns>
        public static string GetBillNo()
        {  
            //string fakeYear = string.Format("{0:0000}", DateTime.Now.Year);
            //string fakeMonth = string.Format("{0:00}", DateTime.Now.Month);
            //string fakedDay = string.Format("{0:00}", DateTime.Now.Day);
            //string fakeCode = string.Format("{0:00000000}", increaseNo);

            //string billNoPrefix = string.Empty;
            string billNo = DateTime.Now.ToString("yyyyMMddHHmmss");
            //billNo = string.Format("{4}{0}{1}{2}-{3}", fakeYear, fakeMonth, fakedDay, fakeCode, billNoPrefix);
            return billNo;
        }


    }
}